<?php
//$count_words 变量设置为true时,一个中文字符的字长等于2个英文字母的字长
/*
function smarty_modifier_truncateex( $string,$length = 80,$etc='...',$count_words = true ) {
        mb_internal_encoding("UTF-8");
        if ($length == 0)return '';
        if ( strlen( $string ) <= $length ) return $string;
        preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $info); 
   if( $count_words ){
                $j = 0;
                for($i=0; $i<count($info[0]); $i++) {
                        $wordscut .= $info[0][$i];
                        if( ord( $info[0][$i] ) >=128 ){
                                $j = $j+2;
                        }else{
                                $j = $j + 1;
                        }
                        if ($j >= $length ) {
                                        return $wordscut.$etc;
                        }
                }
                return join('', $info[0]);
   }
        return join("",array_slice( $info[0],0,$length ) ).$etc;
}
*/
/*
	* 中文截取，支持gb2312,gbk,utf-8,big5
	*
	* @param string $str 要截取的字串
	* @param int $start 截取起始位置
	* @param int $length 截取长度
	* @param string $charset utf-8|gb2312|gbk|big5 编码
	* @param $suffix 是否加尾缀
	*/

	function smarty_modifier_truncateex($string,$length=80,$etc='...',$count_words = true,$charset="utf-8",$suffix = true) {
		$start = 0;
		if (function_exists ( "mb_substr" ))
			return mb_substr ( $string, $start, $length, $charset );
		$re ['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
		$re ['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
		$re ['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
		$re ['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
		preg_match_all ( $re [$charset], $string, $match );
		$slice = join ( "", array_slice ( $match [0], $start, $length ) );
		if ($suffix)
			return $slice . $etc;
		return $slice;
	}


?>
